//
//  homeInformationViewController.m
//  UI_A项目
//
//  Created by dlios on 15-1-15.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "homeInformationViewController.h"

@interface homeInformationViewController ()

@end

@implementation homeInformationViewController


- (void)dealloc
{
    [_collection release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    
    self.navigationItem.title = self.model.title;
    
    
    UICollectionViewFlowLayout *collectionVFL = [[UICollectionViewFlowLayout alloc] init];
    collectionVFL.itemSize = CGSizeMake(100, 120);
    
    collectionVFL.minimumInteritemSpacing = 1;
    
    collectionVFL.minimumLineSpacing = 40;
    
    collectionVFL.sectionInset = UIEdgeInsetsMake(5, 22, 10, 22);
    
    collectionVFL.footerReferenceSize = CGSizeMake(0, 80);
    
    self.collection = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:collectionVFL];
    self.collection.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"u=213802053,255796087&fm=21&gp=0" ofType:@"jpg"]]];
    [self.view addSubview:self.collection];
    [self.collection release];
   
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[homeInformationCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    
    
    
    
    [self AFN];
}

- (void)AFN
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_tag_list?category=%@&device=iPhone&type=album", self.model.name];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        NSLog(@"0000%@", responseObject);
        [netWorkManager stopMonitoring];
        NSMutableDictionary *homeDic = responseObject;
        
        self.homeArray = [NSMutableArray array];
        NSMutableArray *homeArray = [homeDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in homeArray) {
            HomeModel *model = [[HomeModel alloc] init];
            model.tname = [dic objectForKey:@"tname"];
            model.cover_path = [dic objectForKey:@"cover_path"];
            //            NSLog(@"title = %@", model.title);
            [self.homeArray addObject:model];
        }
        
        [self.collection reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@", error);
        
    }];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    homeInformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    HomeModel *model = [self.homeArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.homeArray.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeClassificationViewController *classificationVC = [[HomeClassificationViewController alloc] init];
    
    HomeModel *model = [self.homeArray objectAtIndex:indexPath.row];
    
    NSString *tname = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)model.tname, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));
    
    classificationVC.model = model;
    classificationVC.name1 = self.model.name;
    classificationVC.name2 = tname;
    
    
    
    [self.navigationController pushViewController:classificationVC animated:YES];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
